using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Entities;
namespace DAL
{
   public class tbDonhangDAL
    {
       private SqlConnection cn = null;
       public tbDonhangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public int Insert(tbDonhang dh)
        {
            SqlCommand cm = new SqlCommand("Don_hang_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Makh",dh.Makh);
            cm.Parameters.AddWithValue("@Matt",dh.Matt);
            cm.Parameters.AddWithValue("@Matinhtrangdh ",dh.Matinhtrangdh);
            cm.Parameters.AddWithValue("@Ngaydathang", dh.Ngaydathang);
            cm.Parameters.AddWithValue("@Ngaygiaohang", dh.Ngaygiaohang);
            cm.Parameters.AddWithValue("@Diachigiao", dh.Diachi);
            cm.Parameters.AddWithValue("@Trigia", dh.Trigia);
            return int.Parse(cm.ExecuteScalar().ToString());
        }
        public void Update(tbDonhang dh)
        {
            SqlCommand cm = new SqlCommand("Don_hang_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Madh", dh.Madh);
            cm.Parameters.AddWithValue("@Makh",dh.Makh);
            cm.Parameters.AddWithValue("@Matt", dh.Matt);
            cm.Parameters.AddWithValue("@Matinhtrangdh",dh.Matinhtrangdh);
            cm.Parameters.AddWithValue("@Ngaydathang", dh.Ngaydathang);
            cm.Parameters.AddWithValue("@Ngaygiaohang", dh.Ngaygiaohang);
            cm.Parameters.AddWithValue("@Diachigiao", dh.Diachi);
            cm.Parameters.AddWithValue("@Trigia", dh.Trigia);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("Don_hang_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Madh", iD);
            cm.ExecuteNonQuery();
        }
      public tbDonhangCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
      {
          SqlCommand cm = new SqlCommand("Don_hang_Get", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Where", where);
          cm.Parameters.AddWithValue("@PageIndex", pageIndex);
          cm.Parameters.AddWithValue("@PageSize", pageSize);
          cm.Parameters.AddWithValue("@OrderBy", orderBy);
          cm.Parameters.AddWithValue("@OrderDirection",orderDirection);
          cm.Parameters.Add(new SqlParameter("@TotalRecords",DbType.Int32)).Direction =ParameterDirection.Output;
          SqlDataReader dr = cm.ExecuteReader();
          tbDonhangCollection dhCollection = new tbDonhangCollection();
          while (dr.Read())
          {
              tbDonhang dh = new tbDonhang();
              dh.Madh= int.Parse(dr.GetValue(0).ToString());
              dh.Makh =int.Parse(dr.GetValue(1).ToString());
              dh.Matt = int.Parse(dr.GetValue(2).ToString());
              dh.Matinhtrangdh = int.Parse(dr.GetValue(3).ToString());
              dh.Ngaydathang = DateTime.Parse(dr.GetValue(4).ToString());
              dh.Ngaygiaohang = DateTime.Parse(dr.GetValue(5).ToString());
              dh.Diachi = (dr.GetValue(6).ToString());
              dh.Trigia = float.Parse(dr.GetValue(7).ToString());
              dhCollection.Add(dh);
          }
          dr.Close();
          totalRecords = int.Parse(
          cm.Parameters["@TotalRecords"].Value.ToString());
          return dhCollection;
      }
    }
}
